﻿

using HBase;

using MongoDB.Bson;
using MongoDB.Driver;
using hCommand.entity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using hCommand.dto;
using HBase.cache;


namespace WebApi
{
    [FormAuthenticationFilter]
    [RoutePrefix("api/log")]
    public class LoggerController : BaseApiController
    {



        IMServer MGStatuslog = GuidangFactray.getDal<IMServer>();
        [HttpGet, Route("tables")]
        public BaseResult tables()
        {
            var tables = ConnectionCache.queryTables(BaseConfig.BaseDBConnStr);
            return BaseResult.Ok(tables);
        }




        [HttpGet, Route("all/{name}/{begin}/{field}/{type}/{v}")]
        public BaseResult showEquipment(String name, int begin, string field, String type, string v)
        {

            v = WebUtility.UrlDecode(v);

            BsonDocument query = new BsonDocument();

            if (!String.IsNullOrEmpty(field) && !String.IsNullOrEmpty(v))
            {
                if (type == "like")
                {
                    query[field] = new BsonRegularExpression("/" + v + "/");



                }
                else if (type == "eq")
                {

                    query[field] = v;

                }
                else if (type == "gte")
                {

                    query[field] = new BsonDocument();
                    query[field]["$gte"] = v;

                }
                else if (type == "lte")
                {
                    query[field]["$lte"] = v;
                }


            }
            Page<Object> info = MGStatuslog.queryPage(BaseConfig.BaseDBConnStr, name, begin, query);
            return BaseResult.Ok(info);
        }
        [HttpGet, Route("allList/{name}/{begin}/{field}/{type}/{v}")]
        public BaseResult allList(String name, String begin, string field, String type, string v)
        {

            v = WebUtility.UrlDecode(v);

            BsonDocument query = new BsonDocument();

            if (!String.IsNullOrEmpty(field) && !String.IsNullOrEmpty(v))
            {
                if (type == "like")
                {
                    query[field] = new BsonRegularExpression("/" + v + "/");
                }
                else if (type == "eq")
                {
                    query[field] = v;

                }
                else if (type == "gte")
                {

                    query[field] = new BsonDocument();
                    query[field]["$gte"] = v;

                }
                else if (type == "lte")
                {
                    query[field]["$lte"] = v;
                }


            }

            if (!String.IsNullOrEmpty(begin))
            {
                begin = WebUtility.UrlDecode(begin);
                query["time"] = new BsonDocument();
                query["time"]["$gt"] = Convert.ToDateTime(begin);
            }
            BsonDocument sort = new BsonDocument();
            sort["time"] = 1;
            List<BsonDocument> list = MGStatuslog.queryAll("main", name, query, sort);

            List<Object> logList = new List<Object>();
            foreach (BsonDocument log in list)
            {
                logList.Add(JsonUtil.toOb(log));
            }
            return new BaseResult(logList);
        }


        [HttpPost, Route("wmsPage")]
        public BaseResult wmsPage([FromBody] LogDto logDto)
        {

            Page<HttpServerLog> page = MGStatuslog.queryPage<HttpServerLog>(BaseConfig.BaseDBConnStr, logDto);
            return new BaseResult(page);
        }

        [HttpPost, Route("wcsFindEquipmentPage")]
        public BaseResult wcsFindEquipmentPage([FromBody] MessageDto dto)
        {
            String name = dto.tableName;
            if (String.IsNullOrWhiteSpace(name))
            {
                return BaseResult.Error("输入集合名称");
            }
            var info = MGStatuslog.queryPage(BaseConfig.BaseDBConnStr, name, dto);
            return BaseResult.Ok(info);
        }
        [HttpGet, Route("showEquipment/{id}")]
        public BaseResult showEquipment(String id)
        {
            Object info = MGStatuslog.show(BaseConfig.BaseDBConnStr, "status", id);
            return BaseResult.Ok(info);
        }

    }
}
